/**
 * @author smallwang
 * @email justdoalitter@gmail.com
 * @create date 2025-01-30 02:08:37
 * @modify date 2025-01-30 02:08:37
 * @desc [description]
 */
package ltd.smallwang;

import ltd.smallwang.common.ListNode;

public class Leetcode0142 {
  public ListNode detectCycle(ListNode head) {
    if (head == null) {
      return null;
    }
    ListNode slow = head;
    ListNode fast = head;
    ListNode ptr = head;
    while (fast.next != null && fast.next.next != null) {
      slow = slow.next;
      fast = fast.next.next;
      if (slow == fast) {
        while (ptr != slow) {
          ptr = ptr.next;
          slow = slow.next;
        }
        return ptr;
      }
    }
    return null;
  }
}
